/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package trackreggui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeModel;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import webreg.model.Competition;
import webreg.model.exceptions.EmptyDescriptionException;
import webreg.model.exceptions.EmptyPlaceException;
import webreg.model.swing.CompetitionTreeCellRenderer;
import webreg.model.swing.CompetitionTreeModel;

/**
 *
 * @author STKNOLL
 */
public class ModelController {
    private Competition mCompetition;
    private File mRegistrationFile;
    private static ModelController mInstance = null;
    
    /** Constructor */
    private ModelController() {
        mCompetition = new Competition();
        mRegistrationFile = null;
    }

    public static ModelController getInstance() {
        if (mInstance == null) {
            mInstance = new ModelController();
        }
        return mInstance;
    }

    public void setRegistrationFile( File inFile ) {
        mRegistrationFile = inFile;
    }

    public File getRegistrationFile() {
        return mRegistrationFile;
    }

    /** Implementation of the save function
     */
    public void saveCurrentRegistration() {
        OutputStream fos = null;
    	JAXBContext context = null;
	try {
            fos = new FileOutputStream( mRegistrationFile );
            context = JAXBContext.newInstance( Competition.class );
            Marshaller m = context.createMarshaller();
            m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );
            m.marshal( mCompetition, fos );
            fos.close();
	} catch (JAXBException e1) {
            e1.printStackTrace();
	} catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
	}
    }

    /** Load a registration from a file
     */
    public void loadRegistration() {
        JAXBContext context = null;
        try
    	{
            InputStream fis = null;
            fis = new FileInputStream( mRegistrationFile );
            context = JAXBContext.newInstance( Competition.class );
            Unmarshaller um = context.createUnmarshaller();
            mCompetition = (Competition) um.unmarshal(fis);
            fis.close();
	} catch (JAXBException e1) {
            e1.printStackTrace();
	} catch (FileNotFoundException e) {
            e.printStackTrace();
	} catch (IOException e) {
            e.printStackTrace();
	}
    }

    public TreeModel getCompetitionAsTreeModel() {
        //System.out.println("Competition size: " + mCompetition.getAthlets().size());
        CompetitionTreeModel tm = null ;
        tm = new CompetitionTreeModel(mCompetition);
        return tm;
    }

    public DefaultTreeCellRenderer getCompetitionTreeCellRender() {
        return new CompetitionTreeCellRenderer();
    }

    public Competition getCompetition() {
        return mCompetition;
    }

    public void setCompetition(Competition inCompetition) {
        mCompetition = inCompetition;
    }
    
}
